﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for playTimer.xaml
    /// </summary>
    public partial class playTimer : UserControl
    {
        private delegate void clockTick(object state);
        private TimeSpan currentTime;
        private System.Threading.Timer mytimer;
        private clockTick tick;

        //public clockTick Tick
        //{
        //    set { tick = value; }
        //}

        public playTimer()
        {
            InitializeComponent();
            tick = countUpClock;
        }

        public void setTimerToCountUp()
        {
            tick = this.countUpClock;
        }

        public void setTimerToCountDown()
        {
            tick = this.countDownClock;
        }

        public void Start(){
            mytimer = new System.Threading.Timer(new TimerCallback(tick), null, 1000, 1000);

        }
        public void Stop(){
            if(mytimer != null){
                mytimer.Dispose();
                mytimer = null;
            }
        }

        //ENGINEER!!!!
        //THIS FUNCTION WILL COUNT UP
        private void countUpClock(object state)
        {
            //if (currentTime.TotalMilliseconds <= 3600000)
            //{
            //    currentTime = currentTime.Subtract(new TimeSpan(0, 0, 1));
            //    if (currentTime.TotalMilliseconds <= 6000)
            //    {
            //        currentTime = currentTime.Subtract(new TimeSpan(0, 0, 0));

            //    }
            //    //SetTimeDisplayText(currentTime.ToString()); //set label here 

            //    //this.Invalidate();
            //}
        }


        //this function will count down the timer
        private void countDownClock(object state)
        {
            if (currentTime.TotalMilliseconds > 0)
            {
                currentTime = currentTime.Subtract(new TimeSpan(0, 0, 1));
                if (currentTime.TotalMilliseconds <= 0)
                {
                    currentTime = currentTime.Subtract(new TimeSpan(0, 0, 0));

                }
                //SetTimeDisplayText(currentTime.ToString()); //set label here 
                
                //this.Invalidate();
            }
        }


        //private void SetTimeDisplayText(string newTimeDisplayText)
        //{
        //    if (this.InvokeRequired)
        //    {
        //        StringMethod method = new StringMethod(SetTimeDisplayText);
        //        this.BeginInvoke(method, new object[] { newTimeDisplayText });
        //    }
        //    else
        //    {
        //        timeDisplay.Text = newTimeDisplayText;
        //    }
        //}
    }
}
